module.exports = {
	reqPut(url, params) {
		return ajaxPromise({
			url,
			type: "PUT",
			data: params
		})
	},

	reqGet(url, params) {
		return ajaxPromise({
			url,
			data: params
		})
	},

	reqPost(url, params) {
		return ajaxPromise({
			url,
			type: 'POST',
			data: params
		})
	},

	reqUpdate(url, params) {

	},
}

function ajaxPromise(options) {
	let {
		url,
		type = 'GET',
		data = {},
		dataType = 'json',
		contentType = "application/x-www-form-urlencoded",
		processData = true,
	} = options
	if (contentType != "application/x-www-form-urlencoded") {
		processData = false;
		data = typeof data == 'object' ? JSON.stringify(data) : data
	}
	return new Promise((resolve, reject) => {
		$.ajax({
			url,
			type,
			data,
			dataType,
			contentType,
			processData,
			beforeSend: function(xhr) {
				// if (accesstoken && username && userid) {
				// 	xhr.setRequestHeader("AccessToken",
				// 		accesstoken);
				// 	xhr.setRequestHeader("username", username);
				// 	xhr.setRequestHeader("userid", userid);
				// }
			},
			success: function(data) {
				resolve(data)
			},
			error: function(data) {
				resolve(data.responseText)
			}
		})
	})
}